---
title: "19.Rollback_Tables"
---

# Load Packages
```{r}
library(tidyverse)
library(sandwich)
library(stargazer)
```

# Load Data
```{r}
load("Data/data_final.Rdata")
load("Data/gnr_panel.Rdata")
load("Data/ideo_list.Rdata")
```

# Set Covariates (Including Interactions with Each Election Year)
```{r}
covs_list <- c("election", "log_gnr_cumul_23_Z:election", "election:pr_ha_ord", "election:as.factor(DistN)", "election:ln_PopDens_70", "election:Cities_10km", "election:ln_Confs_All", "election:PrRecH_Adm3", "election:Isolados1950", "election:PrWheatAr_1972", "election:LandIneqRat", "election:TerrainSlopeIndex", "election:MedAltitude", "election:Communist_1975", "election:MinDistPCP_Map", "election:med_visit")
```

```{r}
elections <- c("AC75", "AR76", "AR79", "AR80", "AR83", "AR85", "AR87")
```

# Generate First Differences between Every Election Year of Interest (1979-87) and Baseline Year (1976)
```{r}

aggregated_fd <- do.call(rbind, ideo_list[2:7]) %>% left_join(ideo_list[[2]], by = c("DICOFRE_Z", "ideology2")) %>% 
  mutate(FD = votes.x - votes.y) %>%
  select(DICOFRE_Z, ideology2, FD, election.x) %>% 
  rename(election = election.x) %>% 
  pivot_wider(c(DICOFRE_Z, election), names_from = "ideology2", values_from = "FD")

```

# Join vote share first difference and cumulative GNR events up to election year to main data set
```{r}
data_models <- data_final %>% 
  left_join(aggregated_fd) %>% 
  left_join(gnr_panel %>% rename(election = pre_election))
```

# Estimate low LR (Needed to get rid of 1 parish to estimate SE) (App. Table 7bi)
```{r}
models<-list()

data_models2 <- data_models %>% filter(DICOFRE_Z != "140902")

models[[1]] <- lm(as.formula(paste("Communist", paste(c(covs_list), collapse= " + "), sep = " ~ ")),
                data = data_models2 %>% filter(exp_cat2 == 1))
models[[2]]  <- lm(as.formula(paste("Center", paste(c(covs_list), collapse= " + "), sep = " ~ ")),
               data = data_models2 %>% filter(exp_cat2 == 1))
models[[3]]  <- lm(as.formula(paste("Left", paste(c(covs_list), collapse= " + "), sep = " ~ ")),
                data = data_models2 %>% filter(exp_cat2 == 1))
models[[4]]  <- lm(as.formula(paste("Right", paste(c(covs_list), collapse= " + "), sep = " ~ ")),
                data = data_models2 %>% filter(exp_cat2 == 1))

# Robust standard errors
robust_se1 <- sqrt(diag(vcovHC(models[[1]], type = "HC2")))
robust_se2 <- sqrt(diag(vcovHC(models[[2]], type = "HC2")))
robust_se3 <- sqrt(diag(vcovHC(models[[3]], type = "HC2")))
robust_se4 <- sqrt(diag(vcovHC(models[[4]], type = "HC2")))

# Stargazer output 
stargazer(models,
          se = list(robust_se1, robust_se2, robust_se3, robust_se4),
          dep.var.labels = c("Communist", "Center-Left", "Far Left", "Right"),
          type = "text",
          keep = c("electionAR79:log_gnr_cumul_23_Z", "electionAR80:log_gnr_cumul_23_Z", "electionAR83:log_gnr_cumul_23_Z", "electionAR85:log_gnr_cumul_23_Z", "electionAR87:log_gnr_cumul_23_Z"), 
          covariate.labels = c("Cumulative GNR Events, 1979", "Cumulative GNR Events, 1980", "Cumulative GNR Events, 1983", "Cumulative GNR Events, 1985", "Cumulative GNR Events, 1987"),
          add.lines = list(c("Observations Per Year", length(models[[1]]$residuals)/6, length(models[[2]]$residuals)/6, length(models[[3]]$residuals)/6, length(models[[4]]$residuals)/6)))

```

# Estimate high LR (App. Table 7bii)
```{r}

models<-list()

models[[1]] <- lm(as.formula(paste("Communist", paste(c(covs_list), collapse= " + "), sep = " ~ ")),
                data = data_models %>% filter(exp_cat2 == 2))
models[[2]]  <- lm(as.formula(paste("Center", paste(c(covs_list), collapse= " + "), sep = " ~ ")),
               data = data_models %>% filter(exp_cat2 == 2))
models[[3]]  <- lm(as.formula(paste("Left", paste(c(covs_list), collapse= " + "), sep = " ~ ")),
                data = data_models %>% filter(exp_cat2 == 2))
models[[4]]  <- lm(as.formula(paste("Right", paste(c(covs_list), collapse= " + "), sep = " ~ ")),
                data = data_models %>% filter(exp_cat2 == 2))

# Robust standard errors
robust_se1 <- sqrt(diag(vcovHC(models[[1]], type = "HC2")))
robust_se2 <- sqrt(diag(vcovHC(models[[2]], type = "HC2")))
robust_se3 <- sqrt(diag(vcovHC(models[[3]], type = "HC2")))
robust_se4 <- sqrt(diag(vcovHC(models[[4]], type = "HC2")))

# Stargazer output 
stargazer(models,
          se = list(robust_se1, robust_se2, robust_se3, robust_se4),
          dep.var.labels = c("Communist", "Center-Left", "Far Left", "Right"),
          type = "text",
          keep = c("electionAR79:log_gnr_cumul_23_Z", "electionAR80:log_gnr_cumul_23_Z", "electionAR83:log_gnr_cumul_23_Z", "electionAR85:log_gnr_cumul_23_Z", "electionAR87:log_gnr_cumul_23_Z"), 
          covariate.labels = c("Cumulative GNR Events, 1979", "Cumulative GNR Events, 1980", "Cumulative GNR Events, 1983", "Cumulative GNR Events, 1985", "Cumulative GNR Events, 1987"),
          add.lines = list(c("Observations Per Year", length(models[[1]]$residuals)/6, length(models[[2]]$residuals)/6, length(models[[3]]$residuals)/6, length(models[[4]]$residuals)/6)))


```


